package com.cskaoyan.order.controlller;

import com.alibaba.fastjson.JSON;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.order.dto.CreateOrderDto;
import com.cskaoyan.order.dto.OrderDetailResponse;
import com.cskaoyan.order.dto.OrderListResponse;
import com.cskaoyan.order.form.CreateOrderRequest;
import com.cskaoyan.order.service.OrderCoreService;
import lombok.AllArgsConstructor;
import org.bouncycastle.cert.ocsp.RespData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.security.x509.RDN;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author MoJunXiang
 * @since 2022/09/21 22:22
 */
@RestController
@RequestMapping("shopping")
public class OrderController {
    // @Autowired
    // JSON
    @Autowired
    OrderCoreService orderCoreService;

    @PostMapping("order")
    public ResponseData createOrder(@RequestBody CreateOrderRequest request, HttpServletRequest servletRequest) {
        // 返回值
        ResponseUtil<Object> responseUtil = new ResponseUtil<>();

        // 给 request 加上 userId
        String user_info = servletRequest.getHeader("user_info");
        Map map = JSON.parseObject(user_info, Map.class);
        String uid = (String) map.get("uid");
        request.setUserId(Long.parseLong(uid));
        CreateOrderDto createOrderDto = orderCoreService.createOrder(request);

        Integer statusCode = createOrderDto.getStatusCode();
        String result = createOrderDto.getResult();
        if (statusCode != 200) {
            return responseUtil.setErrorMsg(result);
        }

        // 否则说明成功，没有报错
        return responseUtil.setData(result);
    }


    @RequestMapping("order")
    public ResponseData getAllOrders(Integer page, Integer size, String sort) {
        OrderListResponse orderListResponse = orderCoreService.getAllOrders(page, size, sort);

        return new ResponseUtil<OrderListResponse>().setData(orderListResponse);
    }


    @RequestMapping("order/{id}")
    public ResponseData searchOrderById(@PathVariable("id") Long orderId, HttpServletRequest request) {
        // 返回值
        ResponseUtil responseUtil = new ResponseUtil();

        String user_info = request.getHeader("user_info");
        Map map = JSON.parseObject(user_info, Map.class);
        long uid = Long.parseLong(((String) map.get("uid")));
        // String username = (String) map.get("username");
        OrderDetailResponse orderDetailResponse = orderCoreService.searchOrderById(orderId, uid);

        return responseUtil.setData(orderDetailResponse);
    }


    @PostMapping("cancelOrder")
    public ResponseData cancelOrder(@RequestBody Map<String, String> map) {
        String orderId = map.get("id");

        orderCoreService.cancelOrder(orderId);

        return new ResponseUtil().setData("成功！");
    }


    @DeleteMapping("order/{id}")
    public ResponseData deleteOrder(@PathVariable Map<String, String> map) {

        String orderId = map.get("id");
        orderCoreService.deleteOrder(orderId);

        return new ResponseUtil().setData("成功！");
    }
}
